﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PMS.Logic.BD
{
    public partial class LoginAjax : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String methodName = Request["method"];
            Type type = this.GetType();
            MethodInfo method = type.GetMethod(methodName);
            if (method == null) throw new Exception("method is null");
            try
            {
                method.Invoke(this, null);
            }
            catch
            {
                throw;
            }
        }

        public void checkUser()
        {
            UserInfo authUsr = null;
            string strUserName = Request.Form["account"];
            string strPassword = Request.Form["password"];
            string strIsAuth = "No";
            UserInfo usr = null;
            var userModel = DALSession.Current.View_BD_UserDepInfoDAL.GetListBy(u => (u.UserName == strUserName && u.PassWord == strPassword) || (u.AccountLogIn == strUserName && u.PassWord == strPassword)).FirstOrDefault();
            if (userModel != null)
            {
                usr = new UserInfo() { Id = userModel.ID, Name = userModel.UserName, DepName = userModel.DepName, DepId = userModel.DepId, eMail = userModel.Mail, AccountLogIn = userModel.AccountLogIn };
                Session[OperateContext.InfoKey] = new BasicInfo { UserInfo = usr, AuthUserInfo = authUsr, IsAuth = strIsAuth };
                Session[OperateContext.PermissionKey] = Permission.GetPermByUserId(usr.Id);
                Session[OperateContext.BtnPermissionKey] = Permission.GetBtnPermByUserId(usr.Id);
                #region 写入Cookie
                //2.1.2将用户id加密成字符串
                string strCookieValue = SecurityHelper.EncryptUserInfo(usr.Id.ToString());
                //2.1.3创建cookie
                HttpCookie cookie = new HttpCookie(OperateContext.InfoKey, strCookieValue);
                cookie.Expires = DateTime.Now.AddDays(1);
                Response.Cookies.Add(cookie);
                #endregion
                //如果为授权登录，则需要将授权信息存入Session和cookie中
                HttpCookie cookie1 = new HttpCookie(OperateContext.IsAuth, SecurityHelper.EncryptUserInfo("No"));
                cookie1.Expires = DateTime.Now.AddDays(1);
                Response.Cookies.Add(cookie1);
                //存入授权用户ID

                //查看主题
                if (Request.Cookies[OperateContext.theme] == null || Request.Cookies[OperateContext.theme].Value == "undefined")
                {
                    HttpCookie cookieTheme = new HttpCookie(OperateContext.theme, "gray");
                    cookieTheme.Expires = DateTime.Now.AddYears(1);
                    Response.Cookies.Add(cookieTheme);
                }
                Response.Write(JsonHelper.toDefaultMsg(true,"/Index.aspx"));
            }
            else
            {
                Response.Write(JsonHelper.toDefaultMsg(false, "账号或密码不正确"));
            }

        }
    }
}